/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;
import dao.EmployerDAO;
import java.io.File;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import jaxb.Employers.Employers;
import org.xml.sax.InputSource;
import org.xml.sax.SAXParseException;

/**
 *
 * @author trinhnta
 */
public class XMLTool {

    public static void main(String[] args) {
        GenerateJavaObj("src/java/", "jaxb.Employers", "web/WEB-INF/employers.xsd");
        
    }

   public static void GenerateJavaObj(String output, String packageName, String filepath) {
        try {
            SchemaCompiler sc = XJC.createSchemaCompiler();
            sc.setErrorListener(new ErrorListener() {

                public void error(SAXParseException saxpe) {
                    System.out.println("error" + saxpe.getMessage());
                }

                public void fatalError(SAXParseException saxpe) {
                    System.out.println("fatal" + saxpe.getMessage());
                }

                public void warning(SAXParseException saxpe) {
                    System.out.println("warning" + saxpe.getMessage());
                }

                public void info(SAXParseException saxpe) {
                    System.out.println("info" + saxpe.getMessage());
                }
            });
            sc.forcePackageName(packageName);
            File schema = new File(filepath);
            InputSource is = new InputSource(schema.toURI().toString());
            sc.parseSchema(is);
            S2JJAXBModel model = sc.bind();
            JCodeModel code = model.generateCode(null, null);
            code.build(new File(output));
            System.out.println("Finish");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public static void Marshalling(Object obj, String pathfile) {
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshall = context.createMarshaller();
            marshall.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            marshall.marshal(obj, new File(pathfile));

        } catch (JAXBException ex) {
            System.out.println(ex.getMessage());

        }
    }

    
}
